package com.smfx.yxt.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smfx.yxt.daoSqlserver.OrganizeDepartMapper;
import com.smfx.yxt.daoSqlserver.TeacherInfoMapper;
import com.smfx.yxt.model.OrganizeDepart;
import com.smfx.yxt.model.OrganizeGroup;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.model.TeacherOrganizeGroup;
import com.smfx.yxt.service.IOrganizeDepartService;
import com.smfx.yxt.utils.StringUtil;

@Service("organizeDepartService")
public class OrganizeDepartServiceImpl implements IOrganizeDepartService {

	@Autowired
	private OrganizeDepartMapper organizeDepartMapper;
	@Autowired
	private TeacherInfoMapper teacherInfoMapper;
	
	@Override
	public List<OrganizeDepart> selectListByParam(OrganizeDepart record) {
		return organizeDepartMapper.selectListByParam(record);
	}

	@Override
	public List<OrganizeDepart> selectComboBox() {
		return organizeDepartMapper.selectComboBox();
	}

	@Override
	public List<OrganizeDepart> selectGroupComboBox(OrganizeGroup organizeGroup) {
		return organizeDepartMapper.selectGroupComboBox(organizeGroup);
	}

	@Override
	public List<TeacherOrganizeGroup> fillTeacherOrganize(Integer schoolId) {
		List<OrganizeDepart> list = organizeDepartMapper.selectListByParam(new OrganizeDepart());
		return getOrganizeDepart(list, schoolId);
	}

	@Override
	public List<TeacherOrganizeGroup> fillTeacherOrganizeByPositionIds(String positionIds, Integer schoolId) {
		List<String> positionIdList = Arrays.asList(positionIds.split(","));
		List<OrganizeDepart> list = organizeDepartMapper.selectListByPositionIds(positionIdList);
		return getOrganizeDepart(list, schoolId);
	}

	private List<TeacherOrganizeGroup> getOrganizeDepart(List<OrganizeDepart> list, Integer schoolId) {
		List<TeacherOrganizeGroup> departList = new ArrayList<>();
		if(list != null && list.size() > 0) {
			for(OrganizeDepart menu : list) {
				String key = null;
				Integer parentId = menu.getParentId();
				if(!(parentId != null && parentId != 0)) {
					key = "root";
				}else {
					key = String.valueOf(parentId);
				}
				TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
				if(key.equals("root")) {
					teacherOrganize.setGroupId(menu.getId());
					teacherOrganize.setGroupName(menu.getPosition());
					teacherOrganize.setParentGroupId(menu.getParentId());
					teacherOrganize.setIds(menu.getIds());
					departList.add(teacherOrganize);
				}else {
					teacherOrganize.setGroupId(menu.getId());
					teacherOrganize.setGroupName(menu.getPosition());
					teacherOrganize.setParentGroupId(menu.getParentId());
					teacherOrganize.setIds(menu.getIds());
					for(TeacherOrganizeGroup parent : departList) {
						if(parentId.intValue() == parent.getGroupId().intValue()) {
							parent.setHasChild(true);
							parent.getSubList().add(teacherOrganize);
						}
					}
				}
			}
			List<TeacherInfo> teacherList = teacherInfoMapper.selectAll(schoolId);
			if(teacherList != null && teacherList.size() > 0) {
				for(TeacherInfo teacher : teacherList) {
					for(TeacherOrganizeGroup group : departList) {
						if(StringUtil.isExistPosition(teacher.getPosition(), String.valueOf(group.getGroupId()))) {
							if(!group.isHasChild()) {
								TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
								teacherOrganize.setTeacherId(teacher.getId());
								teacherOrganize.setTeacherName(teacher.getName());
								teacherOrganize.setGroupId(group.getGroupId());
								teacherOrganize.setGroupName(group.getGroupName());
								teacherOrganize.setParentGroupId(group.getParentGroupId());
								group.getSubList().add(teacherOrganize);
							}else {
								for(TeacherOrganizeGroup secondGroup : group.getSubList()) {
									if(StringUtil.isExistPosition(teacher.getPosition(), String.valueOf(secondGroup.getGroupId()))) {
										TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
										teacherOrganize.setTeacherId(teacher.getId());
										teacherOrganize.setTeacherName(teacher.getName());
										teacherOrganize.setGroupId(secondGroup.getGroupId());
										teacherOrganize.setGroupName(secondGroup.getGroupName());
										teacherOrganize.setParentGroupId(secondGroup.getParentGroupId());
										secondGroup.getSubList().add(teacherOrganize);
									}
								}
							}
						}
					}
				}
			}
		}
		return departList;
	}
	
}
